#include "PicturePuzzleLayer.h" 
#include <math.h>
#include "cocos2d.h"
#include "editor-support\cocostudio\CocoStudio.h"
#include "cocos\ui\CocosGUI.h"
#include "..\cocos2d\extensions\cocos-ext.h"
#include "PuzzleGuess.h"
#include "ChoosePictureLayer.h"
#include "PictureItem.h"
#include "PictureItemCell.h"
#include "PicturePuzzleGameArea.h" 
#include "PuzzleGameScoreNode.h"

USING_NS_CC;   
using namespace cocostudio;
USING_NS_GAME;

Scene* PicturePuzzleLayer::createScene(PictureItem &selectedPictureItem)
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = PicturePuzzleLayer::create(selectedPictureItem); 
	 
    // add layer as a child to scene
    scene->addChild(layer); 

    // return the scene
    return scene;
} 

void PicturePuzzleLayer::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{ 
	
	//CCLog(CCString::createWithFormat("onKeyPressed:%d",keyCode)->getCString()); 
}

void PicturePuzzleLayer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
	if(EventKeyboard::KeyCode::KEY_BACKSPACE == keyCode)
	{ 
		auto newScene = ChoosePictureLayer::createScene();
		auto newSceneTransition = cocos2d::TransitionSlideInL::create(0.4,newScene);
		Director::getInstance()->replaceScene(newSceneTransition);
	}
	//CCLog(CCString::createWithFormat("onKeyReleased:%d",keyCode)->getCString()); 
}


// on "init" you need to initialize your instance
bool PicturePuzzleLayer::init(PictureItem &selectedPictureItem)
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
	//if ( !LayerColor::initWithColor(cocos2d::Color4B::GREEN) )
	//if ( !LayerColor::init() )
    {
        return false;
    } 
	
	//this->setTouchEnabled(true); 
	this->setKeypadEnabled(true); 

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();  
	   
	this->pageLayout = dynamic_cast<cocos2d::ui::Layout*>( cocostudio::GUIReader::shareReader()->widgetFromJsonFile("ChoosePicturePanelUI.ExportJson"));
	this->pageLayout->setSize(winSize);
	this->pageLayout->setAnchorPoint(cocos2d::Point::ANCHOR_MIDDLE); 
	this->pageLayout->setPosition(ccp(winSize.width * 0.5, winSize.height * 0.5)); 
	this->addChild(this->pageLayout);  
	
	int gameAreaMarginTop = winSize.height*2/16;
	int gameAreaMarginRight = winSize.width*1/16;
	int gameAreaMarginBottom = winSize.height*1/16;
	int gameAreaMarginLeft = winSize.width*1/16;  
	 
	cocos2d::Size scoreNodeSize(((winSize.width*6*14/(10*16)) * 0.9 * 0.5), gameAreaMarginTop/2 * 1.2);
	auto gameBestScore = com::belyn::game::puzzleGuess::PuzzleGameScoreNode::create();
	gameBestScore->setContentSize(scoreNodeSize); 
	gameBestScore->setAnchorPoint(cocos2d::Point::ANCHOR_MIDDLE);
	gameBestScore->setPosition(cocos2d::Point(winSize.width - gameBestScore->getContentSize().width*0.5 - gameAreaMarginRight, winSize.height + gameBestScore->getContentSize().height*0.6 - gameAreaMarginTop)); 
	gameBestScore->setScoreTitle("Best");   
	this->addChild(gameBestScore);  

	auto gameScore = com::belyn::game::puzzleGuess::PuzzleGameScoreNode::create();
	gameScore->setContentSize(scoreNodeSize); 
	gameScore->setAnchorPoint(cocos2d::Point::ANCHOR_MIDDLE);
	gameScore->setPosition(cocos2d::Point(winSize.width - gameBestScore->getContentSize().width - gameScore->getContentSize().width*0.5 - gameAreaMarginRight - 10, winSize.height + gameScore->getContentSize().height*0.6 - gameAreaMarginTop));  
	this->addChild(gameScore); 

	auto gameArea = com::belyn::game::puzzleGuess::PicturePuzzleGameArea::create();
	cocos2d::Size gameAreaSize(winSize.width - gameAreaMarginLeft - gameAreaMarginRight, winSize.height - gameAreaMarginBottom - gameAreaMarginTop);
	gameArea->setContentSize(gameAreaSize);
	gameArea->setAnchorPoint(cocos2d::Point::ANCHOR_MIDDLE);
	cocos2d::Point gameAreaPoint(gameAreaSize.width*0.5 + gameAreaMarginLeft, gameAreaSize.height*0.5 + gameAreaMarginBottom);
	gameArea->setPosition(gameAreaPoint);
	this->addChild(gameArea);

	cocos2d::Data pData = cocos2d::FileUtils::getInstance()->getDataFromFile(selectedPictureItem.getFilepath());  
	auto pi = new cocos2d::Image();
	pi->autorelease(); 
	pi->initWithImageData(pData.getBytes(),pData.getSize()); 

	gameArea->setGameAreaMargin(gameAreaMarginTop, gameAreaMarginRight, gameAreaMarginBottom, gameAreaMarginLeft);
	gameArea->setPuzzleImageSource(pi);
	
	gameScore->beginGameScore();
	 
    return true; 
}
 
    